// pages/class-list/index.js
Page({
    onLoad() {
        let types = this.data.types;
        types.map(item => {
            item.data = [];
            item.page = {
                isLoading: false,
                isShowLoading: true,
                isComplete: false,
                total: null,
                currentIndex: 1,
                size: 20,
            }
        })
        this.setData({types});
        this.swichNav({currentTarget: {dataset: {index: 0}}}, true);

        wx.$ajax.getSystemOptions()
            .then(({data: {data, msg, status}, header, statusCode, errMsg}) => {
                if (status === '0') {
                    this.setData({
                        tipStatus: data.systemSetting.isCharge
                    })
                }
            })
    },
    /**
     * 页面的初始数据
     */
    data: {
        currentTab: 0,
        types: [
            {
                name: '全部',
                index: 0,
            },
            {
                name: '视频',
                index: 3,
            },
            {
                name: '图文',
                index: 1,
            },
            {
                name: '音频',
                index: 2,
            },
        ],
        tipStatus: '',
    },
    // 切换
    swichNav(e, flag) {
        var cur = e.currentTarget.dataset.index;
        if (this.data.currentTab != cur || flag) {
            this.setData({
                currentTab: cur
            });
            this.scrollLower(e);
        }

    },
    // 轮播改变
    swiperChange(e) {
        this.setData({
            currentTab: e.detail.current
        })
    },
    // 滚动到最下面触发
    scrollLower(e) {
        let lessions = this.data.types;
        let index = e.currentTarget.dataset.index;
        let lession = lessions[index], page = lession.page;
        if (page.isLoading || page.isComplete) {
            console.log(`${page.isLoading ? '正在加载...' : '已加载完成'}`);
        } else {
            wx.$ajax.getLessionList({
                "start": page.currentIndex,
                "size": page.size,
                "courseType": lession.index
            }, {isToken: true})
                .then(({data: {data, msg, status}, header, statusCode, errMsg}) => {
                    if (status === '0') {
                        lession.data = lession.data.concat(data.courseList || []);
                        page.currentIndex++;
                    }
                    page.isLoading = false;
                    page.total = data.total;
                    if (page.total <= lession.data.length) {
                        page.isComplete = true;
                        page.isShowLoading = false;
                    }
                    this.setData({types: lessions})
                })
        }

    }
})